package ddcp;

import static org.junit.Assert.*;

import java.util.NoSuchElementException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestDDCP
{
	private DDCP myDDCP;
	
	@Before public void setUp()
	{
		myDDCP = new DDCP();
	}
	
	@After public void tearDown()
	{
		myDDCP = null;
	}
	
	//*********************** canPerformAction() *********************//
	@Test (expected = NullPointerException.class) public void testCanPerformActionWithNullInputs()
	{
		myDDCP.canPerformAction(null, null);
	}
	
	@Test (expected = NoSuchElementException.class) public void testCanPerformActionWithNonExistantUser()
	{
		User invalidUser = new User();
		myDDCP.canPerformAction( invalidUser, "Destroy");
	}
	
	@Test (expected = IllegalArgumentException.class) public void testCanPerformActionWithNonExistantAction()
	{
		String invalidAction = "Save a million puppies from certain doom";
		myDDCP.canPerformAction(new User(), invalidAction);
	}
	
	@Test public void testCanPerformActionNormalCase()
	{
		User validUser = new User();
		String validAction = "Commit genocide of Lemmings";
		assertTrue( myDDCP.canPerformAction(validUser, validAction) );
	}
	
	//************************** addUser() ****************************//
	@Test (expected = NullPointerException.class) public void testAddUserWithNullInput()
	{
		myDDCP.addUser(null);
	}
	
	@Test public void testAddUserNormalCase()
	{
		User steve = new User();
		myDDCP.addUser(steve);
		assertTrue( myDDCP.myUsers.containsValue(steve) );
	}
	
	//*************************** removeUser() *************************//
	@Test (expected = NullPointerException.class) public void testRemoveUserWithNullInput()
	{
		myDDCP.removeUser(null);
	}
	
	@Test (expected = NoSuchElementException.class) public void testRemoveUserWithNonExistantUser()
	{
		User iDontExist = new User();
		myDDCP.removeUser(iDontExist);
	}
	
	@Test public void testRemoveUserNormalCase()
	{
		User iExist = new User();
		User iShouldBeEqualToIExist = myDDCP.removeUser(iExist);
		assertTrue( iExist.equals(iShouldBeEqualToIExist) );
	}
	
	//***************************** configureDevice() *******************//
	@Test (expected = NullPointerException.class) public void testConfigureDeviceWithNullInput()
	{
		myDDCP.configureDevice(null);
	}
	
	@Test (expected = NoSuchElementException.class) public void testConfigureDeviceWithNonExistantDevice()
	{
		DoomsdayDevice iDontExist = new DoomsdayDevice();
		myDDCP.configureDevice(iDontExist);
	}
	
	@Test (expected = IllegalAccessException.class) public void testConfigureDeviceOnDeviceThatCannotBeConfigured()
	{
		DoomsdayDevice iCannotBeConfiguredForSomeGoodReason = new DoomsdayDevice();
		myDDCP.configureDevice(iCannotBeConfiguredForSomeGoodReason);
	}
	
	@Test public void testConfigureDeviceNormalCase()
	{
		Configuration iAmAlreadyConfigured = new Configuration();
		Configuration iShouldHaveTheSameConfigurationAsTheOtherConfiguration = myDDCP.configureDevice(new DoomsdayDevice());
		assertEquals(iAmAlreadyConfigured, iShouldHaveTheSameConfigurationAsTheOtherConfiguration);
	}
	
	//******************************** activateDevice() *******************//
	@Test (expected = NullPointerException.class) public void testActivateDeviceWithNullInputs()
	{
		myDDCP.activateDevice(null, null);
	}
	
	@Test (expected = NoSuchElementException.class) public void testActivateDeviceWithInvalidDevice()
	{
		DoomsdayDevice invalidDevice = new DoomsdayDevice();
		Configuration validConfig = new Configuration();
		myDDCP.activateDevice(invalidDevice, validConfig);
	}
	
	@Test (expected = IllegalArgumentException.class) public void testActivateDeviceWithConfigurationForWrongDevice()
	{
		DoomsdayDevice validDevice = new DoomsdayDevice();
		Configuration iAmForTheWrongDevice = new Configuration();
		myDDCP.activateDevice(validDevice, iAmForTheWrongDevice);
	}
	
	@Test public void testActivateDeviceNormalCase()
	{
		DoomsdayDevice validDevice = new DoomsdayDevice();
		Configuration validConfig = new Configuration();
		myDDCP.activateDevice(validDevice, validConfig);
	}
	
	//******************************** modifyUser() ***************************//
	@Test (expected = NullPointerException.class) public void testModifyUserWithNullInput()
	{
		myDDCP.modifyUser(null, 0);
	}
	
	@Test (expected = IllegalArgumentException.class) public void testModifyUserWithInvalidUser()
	{
		User invalidUser = new User();
		myDDCP.modifyUser(invalidUser, 1);
	}
	
	@Test (expected = IllegalArgumentException.class) public void testModifyUserWithInvalidSecurityLevel()
	{
		User validUser = new User();
		myDDCP.modifyUser(validUser, 9000);
	}
	
	@Test public void testModifyUserNormalCase()
	{
		User validUser = new User();
		int validSecLevel = 4;
		User whatModifiedUserShouldLookLike = new User();
		User modifiedUser = myDDCP.modifyUser(validUser, validSecLevel);
		assertEquals(whatModifiedUserShouldLookLike, modifiedUser);
	}
}
